package cn.zhf.shop.controller;

import cn.zhf.shop.entity.Message;
import cn.zhf.shop.entity.organization.Organization;
import cn.zhf.shop.entity.search.OrderSearch;
import cn.zhf.shop.entity.search.OrganizationSearch;
import cn.zhf.shop.mapper.LessonTypeMapper;
import cn.zhf.shop.service.OrganizationService;
import cn.zhf.shop.service.OrganizationTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

@RestController
@Api(tags = "机构管理")
@RequestMapping("/api/organization")
public class OrganizationController {
    @Resource
    private OrganizationService organizationService;

    @Resource
    private LessonTypeMapper lessonTypeMapper;

    @ApiOperation(value = "获得机构列表 服务端")
    @GetMapping("/server/all")
    public Message<?> getAllForServer(){
        List<Organization> organizationList = organizationService.selectAll2();
        List<Organization> organizationList1 = new LinkedList<>();
        for (int i=0;i<organizationList.size();i++)
        {
            Organization organization = organizationList.get(i);
            organization.setTypeName(lessonTypeMapper.selectByPrimaryKey(organization.getOrgType()).getTypeName());
            organizationList1.add(organization);
        }
        return Message.success(null).add(organizationList1);
    }

    @ApiOperation(value = "添加机构")
    @PostMapping("/server/append")
    public Message<Organization> appendOrg(Organization org){
        Date date = new Date();
        org.setCreateDate(date);
        return organizationService.insert(org);
    }

    @ApiOperation(value = "删除机构")
    @PostMapping("/server/remove")
    public Message removeOrg(int orgId){
        return organizationService.deleteByPrimaryKey(orgId);
    }

    @ApiOperation(value = "更新机构")
    @PostMapping("/server/update")
    public Message update(Organization org){
        return organizationService.updateByPrimaryKeySelective(org);
    }

    @ApiOperation(value = "根据机构类型查找")
    @GetMapping("/client/type")
    public Message<List<Organization>> getByType(String orgType){
        return organizationService.selectByOrgType(orgType);
    }

    @ApiOperation(value = "根据机构名称查找")
    @GetMapping("/client/name")
    public Message<List<Organization>> getByName(String orgName){
        return organizationService.selectByOrgName("%"+orgName+"%");
    }

    @ApiOperation(value = "根据负责人查找")
    @GetMapping("/server/head")
    public Message<List<Organization>> getByHead(int orgHead){
        return organizationService.selectByHead(orgHead);
    }

    @ApiOperation(value = "根据Id查找")
    @GetMapping("/server/id")
    public Message<Organization> getById(int orgId){
        return organizationService.selectByPrimaryKey(orgId);
    }

    @ApiOperation(value = "精确查询")
    @GetMapping("/search1")
    public Message<List<Organization>> selectSearch1(String orgName,Integer orgType){
        return organizationService.selectSearch1(orgName,orgType);
    }

    @ApiOperation(value = "模糊查询")
    @GetMapping("/search2")
    public Message<List<Organization>> selectSearch2(String str){
        return organizationService.selectSearch2(str);
    }

    @ApiOperation(value = "根据推荐人id返回所有机构")
    @GetMapping("/referenceId")
    public Message<List<Organization>> selectBrReferenceId(Integer referenceId){
        return organizationService.selectByReferenceId1(referenceId);
    }

    @ApiOperation(value = "多条件组合模糊查询")
    @PostMapping("/mohu/condition/search")
    public Message mohuConditionSearch(OrganizationSearch c){
        return organizationService.mohuConditionSearch(c);
    }

}
